<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use app\traits\MyModelTrait;

/**
 * @mixin \think\Model
 */
class AdminPermission extends Model
{
    use MyModelTrait;

    // 访问权限
    protected $table = 'admin_permission';

    /**
     * 生成日志
     *
     * @param string    $title
     * @param string    $uri
     * @param int       $pid
     * @param string    $path
     * @return void
     */
    public static function generateLog(string $title, string $uri, int $pid = 0, string $path = ''): void
    {
        $insert = [
            'title'         => $title,
            'uri'           => $uri,
            'pid'           => $pid,
            'path'          => $path,
            'created_at'    => date('Y-m-d H:i:s'),
            'updated_at'    => date('Y-m-d H:i:s'),
        ];
        self::insert($insert);
    }// generateLog() end

    /**
     * 生成 path
     *
     * @param int $pid
     * @return string
     */
    public static function generatePath(int $pid = 0): string
    {
        $path = self::whereRaw("id = :id", ['id' => $pid])->value('Path') ?? "0";
        // 0/1
        if ($pid > 0) $path .= "/{$pid}";
        return $path;
    }// generatePath() end

    /**
     * 删除子目录
     *
     * @param int $pid  admin_menu.id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function deleteChildPermission(int $pid)
    {
        $self = self::whereRaw('pid = :pid', ['pid' => $pid])->select();
        foreach($self as $permission){
            $permission_id = (int)$permission->id;
            $permission->delete();
            self::deleteChildmenu($permission_id);
        }
    }// deleteChildPermission() end

    /**
     * 获取访问权限
     * 只有二级
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getPermission(): array
    {
        $data = [];
        $self = self::where('pid', 0)->column('title', 'id');
        foreach($self as $pid => $title){
            $children = self::where('pid', $pid)->select()->toArray();
            foreach($children as $index => $child){
                unset($children[$index]['path']);
                unset($children[$index]['created_at']);
                unset($children[$index]['updated_at']);

                $children[$index]['title']   = "{$child['title']}({$child['uri']})";
                $children[$index]['field']   = 'title';
                $children[$index]['checked'] = false;

            }
            $data[] = [
                'id'        => $pid,
                'title'     => $title,
                'field'     => 'title',
                'checked'   => false,
                'spread'    => false,
                'children'  => $children,
            ];
        }
        /*array:2 [
           0 => array:4 [
                "id"        => 1
                "title"     => "测试菜单"
                'field'     => 'title'
                'checked'   => false
                'spread'    => true
                "children"  => array:4 [
                    0 => array:3 [
                        "id"        => 4
                        "title"     => "列表(uri)"
                        "pid"       => 1
                        'field'     => 'title'
                        'checked'   => false
                    ]
                ]
          ]
          1 => array:3 [
              "id"        => 2
              "title"     => "测试菜单3"
              'checked'   => false
              'spread'    => true
              "children"  => []
          ]
        ]*/
        return $data;
    }// getPermission() end
}
